<template>
  <div class="dashboard-container">
    <!-- 顶部筛选栏 -->
    <div class="filter-bar">
      <div class="filter-tabs">
        <div class="filter-tab" :class="{ active: timeRange === 'today' }" @click="handleTimeRange('today')">今日</div>
        <div class="filter-tab" :class="{ active: timeRange === 'yesterday' }" @click="handleTimeRange('yesterday')">昨日</div>
        <div class="filter-tab" :class="{ active: timeRange === 'week' }" @click="handleTimeRange('week')">近7日</div>
        <div class="filter-tab" :class="{ active: timeRange === 'month' }" @click="handleTimeRange('month')">近30日</div>
        <div class="filter-tab" :class="{ active: timeRange === 'custom' }" @click="handleTimeRange('custom')">指定日期</div>
      </div>
      <div class="filter-tip">每15分钟自动更新数据，较昨日为对比昨日同时刻</div>
    </div>

    <!-- 顶部统计卡片 -->
    <el-row :gutter="20" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-revenue">
            <svg-icon icon-class="money" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">今日销售额</div>
            <count-to :start-val="0" :end-val="12799" :duration="2600" prefix="¥" class="card-panel-num"/>
            <div class="card-panel-trend">
              <span class="trend-text">较昨日</span>
              <span class="trend-value up">+15.2%</span>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-orders">
            <svg-icon icon-class="shopping" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">今日订单数</div>
            <count-to :start-val="0" :end-val="368" :duration="2800" class="card-panel-num"/>
            <div class="card-panel-trend">
              <span class="trend-text">较昨日</span>
              <span class="trend-value down">-8.4%</span>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-users">
            <svg-icon icon-class="peoples" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">新增用户</div>
            <count-to :start-val="0" :end-val="128" :duration="3000" class="card-panel-num"/>
            <div class="card-panel-trend">
              <span class="trend-text">较昨日</span>
              <span class="trend-value up">+22.5%</span>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-price">
            <svg-icon icon-class="money" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">客单价</div>
            <count-to :start-val="0" :end-val="368" :duration="3200" prefix="¥" class="card-panel-num"/>
            <div class="card-panel-trend">
              <span class="trend-text">较昨日</span>
              <span class="trend-value up">+12.5%</span>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 销售趋势图表 -->
    <el-row :gutter="20" class="chart-row">
      <el-col :xs="24" :sm="24" :lg="16">
        <div class="chart-wrapper">
          <div class="chart-header">
            <div class="header-left">
              <h3>销售趋势</h3>
              <el-tag size="small" type="info" class="time-tag">{{ timeRangeText }}</el-tag>
            </div>
            <el-dropdown @command="handleTimeRangeChange" trigger="click">
              <span class="el-dropdown-link">
                {{ timeRangeText }}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="week">本周</el-dropdown-item>
                <el-dropdown-item command="month">本月</el-dropdown-item>
                <el-dropdown-item command="year">本年</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <v-chart :options="salesChart" autoresize />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <div class="chart-header">
            <div class="header-left">
              <h3>热门商品</h3>
              <el-tag size="small" type="info" class="time-tag">{{ periodText }}</el-tag>
            </div>
            <el-dropdown @command="handlePeriodChange" trigger="click">
              <span class="el-dropdown-link">
                {{ periodText }}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="week">本周</el-dropdown-item>
                <el-dropdown-item command="month">本月</el-dropdown-item>
                <el-dropdown-item command="year">本年</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <div class="hot-products">
            <div v-for="(item, index) in hotProducts" :key="index" class="hot-item">
              <div class="hot-rank" :class="{ 'top-3': index < 3 }">{{ index + 1 }}</div>
              <div class="hot-info">
                <div class="hot-name">{{ item.name }}</div>
                <div class="hot-stats">
                  <span class="hot-sales">销量 {{ item.sales }}件</span>
                  <span class="hot-amount">¥{{ item.amount }}</span>
                </div>
                <el-progress :percentage="item.percentage" :color="item.color" :show-text="false" :stroke-width="4"/>
              </div>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 实时数据和排行榜 -->
    <el-row :gutter="20" class="data-row">
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="data-wrapper">
          <div class="chart-header">
            <div class="header-left">
              <h3>实时交易</h3>
              <el-tag size="small" type="success">实时</el-tag>
            </div>
            <el-dropdown @command="handleOrderTypeChange" trigger="click">
              <span class="el-dropdown-link">
                全部订单<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="all">全部订单</el-dropdown-item>
                <el-dropdown-item command="paid">已付款</el-dropdown-item>
                <el-dropdown-item command="unpaid">未付款</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <div class="live-list">
            <div v-for="(item, index) in liveOrders" :key="index" class="live-item">
              <el-avatar :size="32" :src="item.avatar">{{ item.name.substr(0, 1) }}</el-avatar>
              <div class="live-info">
                <div class="live-title">
                  <span class="name">{{ item.name }}</span>
                  <span class="action">购买了</span>
                  <span class="product">{{ item.product }}</span>
                </div>
                <div class="live-meta">
                  <span class="live-time">{{ item.time }}</span>
                  <el-tag size="mini" :type="item.status === 'paid' ? 'success' : 'warning'">
                    {{ item.status === 'paid' ? '已付款' : '未付款' }}
                  </el-tag>
                </div>
              </div>
              <div class="live-amount">¥{{ item.amount }}</div>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="data-wrapper">
          <div class="chart-header">
            <div class="header-left">
              <h3>商品排行</h3>
              <el-tag size="small" type="info" class="time-tag">{{ rankPeriodText }}</el-tag>
            </div>
            <el-dropdown @command="handleRankChange" trigger="click">
              <span class="el-dropdown-link">
                {{ rankTypeText }}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="sales">销售额</el-dropdown-item>
                <el-dropdown-item command="visits">访问量</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <div class="rank-list">
            <div v-for="(item, index) in rankList" :key="index" class="rank-item">
              <span class="rank-number" :class="{ 'top-3': index < 3 }">{{ index + 1 }}</span>
              <div class="rank-info">
                <span class="rank-title">{{ item.title }}</span>
                <div class="rank-meta">
                  <span class="rank-label">{{ rankType === 'sales' ? '销售额' : '访问量' }}</span>
                  <span class="rank-value">{{ rankType === 'sales' ? '¥' + item.sales : item.visits }}</span>
                </div>
                <el-progress :percentage="getPercentage(item)" :color="getProgressColor(index)" :show-text="false" :stroke-width="4"/>
              </div>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import CountTo from 'vue-count-to'
import ECharts from 'vue-echarts'
import 'echarts/lib/chart/line'
import 'echarts/lib/chart/pie'
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/title'
import 'echarts/lib/component/legend'

export default {
  name: 'Dashboard',
  components: {
    CountTo,
    'v-chart': ECharts
  },
  data() {
    return {
      timeRange: 'today',
      timeRangeText: '本周',
      periodText: '本周',
      rankPeriodText: '本周',
      rankType: 'sales',
      rankTypeText: '销售额',
      orderType: 'all',
      hotProducts: [
        { name: 'iPhone 13 Pro Max', sales: 328, amount: '429,900', percentage: 95, color: '#f56c6c' },
        { name: '华为 Mate 50 Pro', sales: 286, amount: '372,800', percentage: 85, color: '#e6a23c' },
        { name: '小米 13', sales: 256, amount: '281,600', percentage: 75, color: '#409eff' },
        { name: 'MacBook Pro', sales: 188, amount: '245,900', percentage: 65, color: '#67c23a' },
        { name: 'AirPods Pro', sales: 156, amount: '187,200', percentage: 55, color: '#909399' }
      ],
      // 销售趋势图表配置
      salesChart: {
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.9)',
          borderColor: '#eee',
          borderWidth: 1,
          textStyle: { color: '#666' },
          padding: [8, 12]
        },
        grid: {
          top: 30,
          left: '3%',
          right: '3%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
          axisLine: {
            lineStyle: { color: '#eee' }
          },
          axisTick: { show: false },
          axisLabel: {
            color: 'rgba(0,0,0,0.45)',
            margin: 12
          }
        },
        yAxis: {
          type: 'value',
          axisLine: { show: false },
          axisTick: { show: false },
          axisLabel: {
            color: 'rgba(0,0,0,0.45)',
            margin: 12
          },
          splitLine: {
            lineStyle: { color: '#f5f5f5' }
          }
        },
        series: [{
          name: '销售额',
          type: 'line',
          smooth: true,
          data: [3200, 4500, 5800, 3800, 6200, 7500, 6800],
          symbolSize: 8,
          itemStyle: {
            color: '#1890ff'
          },
          lineStyle: {
            width: 3
          },
          areaStyle: {
            color: {
              type: 'linear',
              x: 0, y: 0, x2: 0, y2: 1,
              colorStops: [{
                offset: 0,
                color: 'rgba(24,144,255,0.15)'
              }, {
                offset: 1,
                color: 'rgba(24,144,255,0.02)'
              }]
            }
          }
        }]
      },
      // 实时交易数据
      liveOrders: [
        { name: '张三', avatar: '', product: 'iPhone 13 Pro', amount: 8999, time: '2分钟前', status: 'paid' },
        { name: '李四', avatar: '', product: '耐克运动鞋', amount: 599, time: '5分钟前', status: 'unpaid' },
        { name: '王五', avatar: '', product: '小米手环', amount: 299, time: '8分钟前', status: 'paid' },
        { name: '赵六', avatar: '', product: '华为平板', amount: 2399, time: '12分钟前', status: 'paid' },
        { name: '钱七', avatar: '', product: '索尼耳机', amount: 1299, time: '15分钟前', status: 'unpaid' }
      ],
      // 商品排行数据
      rankList: [
        { title: 'iPhone 13 Pro', sales: 289900, visits: 1234 },
        { title: '华为 Mate 40', sales: 258800, visits: 1156 },
        { title: '小米手环', sales: 199900, visits: 956 },
        { title: 'AirPods Pro', sales: 189900, visits: 856 },
        { title: '耐克运动鞋', sales: 179900, visits: 756 }
      ]
    }
  },
  methods: {
    handleTimeRange(range) {
      this.timeRange = range
      // 更新所有数据
      this.updateAllData()
    },
    updateAllData() {
      // 根据选择的时间范围更新所有数据
      // 包括销售额、订单数、用户数等
    },
    handleTimeRangeChange(command) {
      this.timeRangeText = command === 'week' ? '本周' : command === 'month' ? '本月' : '本年'
      // 更新图表数据
    },
    handlePeriodChange(command) {
      this.periodText = command === 'week' ? '本周' : command === 'month' ? '本月' : '本年'
      // 更新热门商品数据
    },
    handleRankChange(command) {
      this.rankType = command
      this.rankTypeText = command === 'sales' ? '销售额' : '访问量'
      // 更新排行榜数据
    },
    handleOrderTypeChange(command) {
      this.orderType = command
      // 更新订单列表
    },
    getPercentage(item) {
      const maxValue = Math.max(...this.rankList.map(i => this.rankType === 'sales' ? i.sales : i.visits))
      const value = this.rankType === 'sales' ? item.sales : item.visits
      return (value / maxValue) * 100
    },
    getProgressColor(index) {
      const colors = ['#f56c6c', '#e6a23c', '#409eff', '#67c23a', '#909399']
      return colors[index] || '#909399'
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  min-height: calc(100vh - 84px);
}

.filter-bar {
  background: #fff;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;

  .filter-tabs {
    display: flex;
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 8px;
    
    &::-webkit-scrollbar {
      display: none;
    }
  }

  .filter-tab {
    padding: 6px 16px;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
    cursor: pointer;
    position: relative;
    flex-shrink: 0;

    &.active {
      color: #1890ff;
      font-weight: 500;

      &::after {
        content: '';
        position: absolute;
        bottom: -4px;
        left: 50%;
        transform: translateX(-50%);
        width: 16px;
        height: 2px;
        background: #1890ff;
        border-radius: 1px;
      }
    }
  }

  .filter-tip {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.45);
    line-height: 1.5;
  }
}

.panel-group {
  padding: 16px;

  .card-panel {
    position: relative;
    height: 120px;
    padding: 24px;
    border-radius: 12px;
    cursor: pointer;
    background: #fff;
    box-shadow: 0 1px 2px 0 rgba(0,0,0,.03), 0 1px 6px -1px rgba(0,0,0,.02), 0 2px 4px 0 rgba(0,0,0,.02);
    transition: all 0.3s;

    &:hover {
      box-shadow: 0 4px 12px rgba(0,0,0,0.1);
      transform: translateY(-2px);
    }

    .card-panel-icon-wrapper {
      float: left;
      width: 64px;
      height: 64px;
      border-radius: 16px;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: all 0.3s;

      &.icon-revenue {
        background: linear-gradient(135deg, #36CFFF 0%, #0B7EFF 100%);
      }
      &.icon-orders {
        background: linear-gradient(135deg, #FF6B6B 0%, #FF3366 100%);
      }
      &.icon-users {
        background: linear-gradient(135deg, #37E2D5 0%, #00B894 100%);
      }
      &.icon-price {
        background: linear-gradient(135deg, #975FE4 0%, #7C4FBF 100%);
      }
    }

    .card-panel-icon {
      font-size: 32px;
      color: #fff;
    }

    .card-panel-description {
      float: right;
      text-align: right;

      .card-panel-text {
        font-size: 14px;
        color: rgba(0,0,0,0.45);
        margin-bottom: 8px;
      }

      .card-panel-num {
        font-size: 28px;
        font-weight: 600;
        color: rgba(0,0,0,0.85);
        margin-bottom: 8px;
      }

      .card-panel-trend {
        font-size: 13px;
        
        .trend-text {
          color: rgba(0,0,0,0.45);
          margin-right: 8px;
        }

        .trend-value {
          &.up {
            color: #52c41a;
            &::before {
              content: "↑";
              margin-right: 4px;
            }
          }
          &.down {
            color: #ff4d4f;
            &::before {
              content: "↓";
              margin-right: 4px;
            }
          }
        }
      }
    }
  }
}

.chart-row {
  padding: 0 16px;
  margin-bottom: 16px;
}

.data-row {
  padding: 0 16px;
  margin-bottom: 16px;
}

.chart-wrapper, .data-wrapper {
  background: #fff;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 1px 2px 0 rgba(0,0,0,.03), 0 1px 6px -1px rgba(0,0,0,.02), 0 2px 4px 0 rgba(0,0,0,.02);
  height: 100%;
  min-height: 400px;

  :deep(.echarts) {
    width: 100% !important;
    height: calc(100% - 60px) !important;
  }

  .chart-header, .data-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;

    h3 {
      margin: 0;
      font-size: 16px;
      font-weight: 500;
      color: rgba(0,0,0,0.85);
    }
  }
}

.live-list {
  .live-item {
    display: flex;
    align-items: center;
    padding: 16px 0;
    border-bottom: 1px solid #f0f0f0;

    &:last-child {
      border-bottom: none;
    }

    .live-info {
      flex: 1;
      margin: 0 16px;

      .live-title {
        margin-bottom: 4px;
        
        .name {
          color: #1890ff;
        }
        .action {
          margin: 0 4px;
          color: rgba(0,0,0,0.45);
        }
        .product {
          color: rgba(0,0,0,0.85);
        }
      }

      .live-meta {
        display: flex;
        align-items: center;
        font-size: 12px;
        color: rgba(0,0,0,0.45);
        margin-top: 4px;

        .live-time {
          margin-right: 8px;
        }

        .el-tag {
          height: 20px;
          line-height: 18px;
          padding: 0 6px;
        }
      }
    }

    .live-amount {
      font-weight: 500;
      color: rgba(0,0,0,0.85);
    }
  }
}

.rank-list {
  .rank-item {
    display: flex;
    align-items: center;
    padding: 12px 0;
    
    .rank-number {
      width: 20px;
      margin-right: 16px;
      color: #909399;
      font-size: 14px;
      text-align: center;

      &.top-3 {
        &:nth-child(1) { color: #f56c6c; }
        &:nth-child(2) { color: #e6a23c; }
        &:nth-child(3) { color: #409eff; }
      }
    }

    .rank-info {
      flex: 1;
      margin-left: 16px;

      .rank-title {
        font-size: 14px;
        color: rgba(0,0,0,0.85);
        margin-bottom: 8px;
        display: block;
      }

      .rank-meta {
        display: flex;
        justify-content: space-between;
        margin-bottom: 8px;
        font-size: 13px;

        .rank-label {
          color: rgba(0,0,0,0.45);
        }

        .rank-value {
          color: #f56c6c;
          font-weight: 500;
        }
      }

      .el-progress {
        margin-top: 4px;
      }
    }
  }
}

.hot-products {
  .hot-item {
    display: flex;
    align-items: center;
    padding: 16px 0;
    border-bottom: 1px solid #f0f0f0;

    &:last-child {
      border-bottom: none;
    }

    .hot-rank {
      width: 32px;
      height: 32px;
      line-height: 32px;
      text-align: center;
      border-radius: 4px;
      margin-right: 16px;
      font-size: 16px;
      font-weight: 600;
      color: #fff;
      background: #909399;

      &.top-3 {
        &:nth-child(1) { background: #f56c6c; }
        &:nth-child(2) { background: #e6a23c; }
        &:nth-child(3) { background: #409eff; }
      }
    }

    .hot-info {
      flex: 1;

      .hot-name {
        font-size: 14px;
        color: rgba(0,0,0,0.85);
        margin-bottom: 8px;
      }

      .hot-stats {
        display: flex;
        justify-content: space-between;
        margin-bottom: 8px;
        font-size: 13px;

        .hot-sales {
          color: rgba(0,0,0,0.45);
        }

        .hot-amount {
          color: #f56c6c;
          font-weight: 500;
        }
      }

      .el-progress {
        margin-top: 4px;
      }
    }
  }
}

.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
  font-size: 13px;
  padding: 5px 8px;
  border-radius: 4px;
  transition: all 0.3s;
  
  &:hover {
    background: rgba(64,158,255,0.1);
  }
  
  .el-icon-arrow-down {
    font-size: 12px;
  }
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;

  .header-left {
    display: flex;
    align-items: center;

    h3 {
      margin: 0;
      font-size: 16px;
      font-weight: 500;
      color: rgba(0,0,0,0.85);
      margin-right: 12px;
    }

    .time-tag {
      background: #f5f5f5;
      border: none;
      color: rgba(0,0,0,0.65);
    }
  }
}

.live-meta {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: rgba(0,0,0,0.45);
  margin-top: 4px;

  .live-time {
    margin-right: 8px;
  }

  .el-tag {
    height: 20px;
    line-height: 18px;
    padding: 0 6px;
  }
}

.rank-info {
  flex: 1;
  margin-left: 16px;

  .rank-title {
    font-size: 14px;
    color: rgba(0,0,0,0.85);
    margin-bottom: 8px;
    display: block;
  }

  .rank-meta {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 13px;

    .rank-label {
      color: rgba(0,0,0,0.45);
    }

    .rank-value {
      color: #f56c6c;
      font-weight: 500;
    }
  }

  .el-progress {
    margin-top: 4px;
  }
}

@media screen and (max-width: 768px) {
  .filter-bar {
    position: sticky;
    top: 0;
    z-index: 10;
  }

  .panel-group {
    padding: 12px;
  }

  .chart-row,
  .data-row {
    padding: 0 12px;
  }

  .card-panel {
    height: auto !important;
    padding: 16px !important;
  }

  .chart-wrapper, .data-wrapper {
    padding: 16px;
    min-height: 300px;
  }

  .chart-header {
    flex-direction: column;
    align-items: flex-start;

    .header-left {
      margin-bottom: 12px;
    }
  }

  .live-item {
    .live-info {
      margin: 0 12px;
    }
  }
}
</style>

